<template>
  <div style="margin-top: 20px;padding-bottom: 20px">
    <!-- 条件查询 -->
    <div class="head-container">
      <div>
        <!-- 搜索 -->
        <el-input
          v-model="query.sourceTableName"
          clearable
          size="small"
          placeholder="请输入表名"
          style="width: 200px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <el-date-picker
          v-model="query.bizDate"
          type="date"
          placeholder="选择数据日期"
          style="margin: 0px 10px"
          value-format="yyyyMMdd"
        />
        <el-select
          v-model="query.status"
          clearable
          size="small"
          placeholder="迁移状态"
          class="filter-item"
          style="width: 120px"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in statusList"
            :key="item.key"
            :label="item.label"
            :value="item.key"
          />
        </el-select>
        <rrOperation :crud="crud" />
      </div>
    </div>
    <el-table
      ref="table"
      v-loading="crud.loading"
      :data="crud.data"
      max-height="350"
      @select="crud.selectChange"
      @select-all="crud.selectAllChange"
      @selection-change="crud.selectionChangeHandler"
    >
      <el-table-column :show-overflow-tooltip="true" label="数据日期" prop="bizDate" width="80" />
      <el-table-column :show-overflow-tooltip="true" label="源服务器" prop="sourceConnName" />
      <el-table-column :show-overflow-tooltip="true" label="源库/表名" prop="sourceDbAndTableName" />
      <el-table-column :show-overflow-tooltip="true" label="目标服务器" prop="targetConnName" />
      <el-table-column :show-overflow-tooltip="true" label="目标库/表名" prop="targetDbAndTableName" />
      <el-table-column :show-overflow-tooltip="true" label="迁移状态" prop="status" width="80">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 'SUCCESS'" type="success">成功</el-tag>
          <el-tag v-else-if="scope.row.status == 'FAIL'" type="warning">失败</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="startTime" label="开始时间" width="160">
        <template slot-scope="scope">
          <div>
            <i class="el-icon-time" />
            <span style="margin-left: 5px">{{ scope.row.startTime }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="endTime" label="结束时间" width="160">
        <template slot-scope="scope">
          <div>
            <i class="el-icon-time" />
            <span style="margin-left: 5px">{{ scope.row.endTime }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" label="数据分析结果" prop="analysisResult" width="120">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="showAnalysisResult(scope.row)">分析结果</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" prop="zhHis">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="zhHandleClick(scope.row)">迁移日志</el-button>
          <el-button v-if="scope.row.exportDataFileResult!==null" type="text" size="small" @click="showExportDataFileResult(scope.row)">下数情况</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style=" float: right;">
      <!--分页组件-->
      <pagination />
    </div>

    <el-dialog
      append-to-body
      :close-on-click-modal="false"
      :visible.sync="showDataTransferLogFlag"
      title="数据迁移日志"
      width="1200px"
      top="5vh"
    >
      <monaco-editor
        ref="convertMonaco"
        v-model="dataTransferErrorMessage"
        style="height: 430px"
      />
    </el-dialog>

    <el-dialog
      append-to-body
      :close-on-click-modal="false"
      :visible.sync="showAnalysisResultFlag"
      title="数据分析结果"
      width="1200px"
      top="5vh"
    >
      <monaco-editor
        ref="convertMonaco"
        v-model="analysisResult"
        style="height: 430px"
      />
    </el-dialog>

    <el-dialog
      append-to-body
      :close-on-click-modal="false"
      :visible.sync="showExportDataFileResultFlag"
      title="下数情况"
      width="1200px"
      top="5vh"
    >
      <monaco-editor
        ref="convertMonaco"
        v-model="exportDataFileResult"
        style="height: 430px"
      />
    </el-dialog>
  </div>
</template>
<script>
import CRUD, { crud, form, header, presenter } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import pagination from '@crud/Pagination'
import MonacoEditor from '@/components/MonacoEditor/monacoEditor'

const defaultCrud = CRUD({ title: '脚本转换最终成果', url: 'api/data/transfer/log' })
const defaultForm = {}

export default {
  name: 'DataTransferLog',
  components: { pagination, rrOperation, MonacoEditor },
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()],
  props: {
    dataTransferTaskPkId: {
      required: true,
      type: String
    }
  },
  data() {
    return {
      showDataTransferLogFlag: false,
      dataTransferErrorMessage: null,
      showAnalysisResultFlag: false,
      analysisResult: null,
      showExportDataFileResultFlag: false,
      exportDataFileResult: '',
      statusList: [
        { key: 'SUCCESS', label: '成功' },
        { key: 'FAIL', label: '失败' },
        { key: 'PROCESSING', label: '转换中' }
      ]
    }
  },
  methods: {
    [CRUD.HOOK.beforeRefresh](crud) {
      crud.query.dataTransferTaskPkId = this.dataTransferTaskPkId
    },
    showAnalysisResult(row) {
      this.showAnalysisResultFlag = true
      this.analysisResult = row.analysisResult
    },
    zhHandleClick(row) {
      this.showDataTransferLogFlag = true
      this.dataTransferErrorMessage = row.errorMessage
    },
    showExportDataFileResult(row) {
      this.showExportDataFileResultFlag = true
      this.exportDataFileResult = row.exportDataFileResult
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
::v-deep .el-date-editor input {
  position: relative;
  bottom: 4px;
  height: 30.5px;
}

::v-deep .el-date-editor .el-icon-date {
  position: relative;
  bottom: 4px;
}
</style>
